package com.achievo.know.web.myfile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.Callable;

import com.achievo.know.component.impl.FileManagerConfig;
import com.achievo.know.domain.FileAttachment;
import com.achievo.know.pojo.Constants;

public class ConvertThread implements Callable<Boolean> {

	private FileManagerConfig config;
	private FileAttachment atts;

	public ConvertThread(FileManagerConfig config, FileAttachment atts) {
		super();
		this.config = config;
		this.atts=atts;
	}

	@Override
	public Boolean call() throws Exception {
		String sourcePath = config.getPersistFileStoreDirectory()
				+ java.io.File.separator + atts.getAttachPath()
				+ atts.getGuid() + "." + atts.getAttachType().getName();
		String destDesk = config.getSwfFileStoreDirectory()
				+ java.io.File.separator + atts.getGuid();
		java.io.File desk = new java.io.File(destDesk);
		if (!desk.exists()) {
			desk.mkdir();
		}
		String destPath = destDesk + java.io.File.separator + "%.swf";
		String cmd = config.getConfigHolder().getConfig(Constants.PDF2SWF)
				+ " " + sourcePath + " -o " + destPath
				+ " -T 9 -f -S -s languagedir="
				+ config.getConfigHolder().getConfig(Constants.XPDF);
		Process pro = Runtime.getRuntime().exec(cmd);

		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(pro.getInputStream()));
		String line=null;
        while ((line=bufferedReader.readLine() )!= null) System.out.println(line);   
		try {
			pro.waitFor();
		} catch (InterruptedException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
